package danran.rpc.common.util;

import com.sun.management.OperatingSystemMXBean;

import java.lang.management.ManagementFactory;

/**
 * @Classname SystemUtil
 * @Description TODO
 * @Date 2021/9/30 18:49
 * @Created by ASUS
 */
public class SystemUtil {

    public static String OS_NAME() {
        return System.getProperty("os.name");
    }

    /**
     * 获取系统cpu负载
     */
    public static double CPU_LOAD(){
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        return osmxb.getSystemCpuLoad();
    }

    /**
     * 获取jvm线程负载
     */
    public static double getProcessCpuLoad(){
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        return osmxb.getProcessCpuLoad();
    }

    /**
     * 获取总的物理内存
     */
    public static long getTotalMemorySize(){
        int kb = 1024;
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        // 总的物理内存
        return osmxb.getTotalPhysicalMemorySize() / kb;
    }

    /**
     * 获取剩余的物理内存
     */
    public static long getFreePhysicalMemorySize(){
        int kb = 1024;
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        // 剩余的物理内存
        return osmxb.getFreePhysicalMemorySize() / kb;
    }

    /**
     * 获取已使用的物理内存
     */
    public static long getUsedMemory(){
        int kb = 1024;
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        //已使用的物理内存
        return (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / kb;
    }

    public static double STORAGE_LOAD() {
        return getUsedMemory() / (getTotalMemorySize() * 1.0);
    }
}
